uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
Breath of Fire IV Technical Guide to Enemies v1.0.
Copyright 2023 by Navarchos
nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn



--------------------------------------------------------------------------------

Table of Contents

--------------------------------------------------------------------------------

0. Credits                                      -- [CRED]

1. Getting Started                              -- [STRT]
    1.1. File extraction
    1.2. File modification
    1.3. Creating a PPF patch

2. Folder and file structure                    -- [FSTR]
    2.1. .EMI file structure

3. Enemy formation data                         -- [FORM]

4. Enemy data                                   -- [DATA]
    5.1. Name
    5.2. ID
    5.3. Type
    5.4. Action weights
    5.5. Default behavior and status
    5.6. Zenny, EXP and Unknown I
    5.7. Initial ability set
    5.8. HP
    5.9. Steal and drop
    5.10. Resistances and Unknown IV
    5.11. Shield points

5. AI                                           -- [AINT]
    6.1. Conditions
    6.2. Actions
    6.3. Messages

6. Special bosses                               -- [BOSS]

7. Changelog                                    -- [CLOG]



[CRED]--------------------------------------------------------------------------

0. Credits

--------------------------------------------------------------------------------

- slivers7/ZC Liu: Without their Enemy and Information Guide, a lot of the work
  figuring out enemy AI would have taken me much longer. Link to their guide:
  https://gamefaqs.gamespot.com/ps/196818-breath-of-fire-iv/faqs/12971
- second_advent: For the formatting and terminology used in this database, which
  is based on their Breath of Fire III Enemy Database. Link to their guide:
  https://gamefaqs.gamespot.com/ps/196817-breath-of-fire-iii/faqs/57525
- ForteGSOmega: For their efforts in hacking this game over 13 years ago. The
  list of enemies they compiled helped tremendously when I was hacking the game 
  myself. Unfortunately, the website that hosted his notes is no longer live. 
  An archive can be found here: https://www.mediafire.com/file/e315n3d3h1dgj21



[STRT]--------------------------------------------------------------------------

1. Getting started

--------------------------------------------------------------------------------

This guide is intended to work with the North American release of the game.

Some of the tools you will need:
    - A hexadecimal editor 
    - dumpsxiso (https://github.com/Lameguy64/mkpsxiso)

And optionally:
    - Ghidra (https://ghidra-sre.org/)
    - psx-mode2 (https://www.romhacking.net/utilities/1417/)
    - mkpsxiso (same link as dumpsxiso)
    - PPFStudio (https://www.romhacking.net/utilities/514/)
    - PCSX-Redux (https://github.com/grumpycoders/pcsx-redux)
    - Sublime Text (https://www.sublimetext.com/)


            ========================================================
                              1.1. File extraction
            ========================================================

You will need dumpsxiso to extract the game's files. To download it, go to the
GitHub link above, and download the latest release. Windows users should select
the win64 option. You will find dumpsxiso.exe and mkpsxiso.exe in the /bin
folder.

    1. Move dumpsxiso.exe to the folder where your .BIN file is kept
    2. Open command prompt in this folder. The easiest way to do this is to type
       'cmd' (without the quotes) in Windows Explorer's address bar while having
       the folder open.
    3. Run 'dumpsxiso.exe <your .BIN file's filename>' (without the quotes) to 
       start extracting the files. If your file name has a space in the name, 
       enclose the file name in quotes.
       3a. If you want to use mkpsxiso to rebuild the game later, use 
           'dumpsxiso.exe <your .BIN file's filename> -s <xml filename>'
           instead. This generates an XML file you can use with mkpsxiso to
           build a new .BIN


            ========================================================
                             1.2. File modification
            ========================================================

There are two ways to insert a modified file back to the game and have a working
.BIN file. For the first method, you need mkpsxiso. To download it, go to the
download link for dumpsxiso. If you have already downloaded it, it should be in
the same /bin folder as dumpsxiso.exe.

    1. Make sure you extracted the .BIN file with dumpsxiso.exe with the XML
       file.
    2. Move mkpsxiso.exe to the folder where you extracted the game's files. 
       Make sure that the XML file is in the same folder.
    3. Overwrite the files you want to change. Make sure that the file's size
       stays the same.
    4. Open command prompt in this folder. The easiest way to do this is to type
       "cmd" (without the quotes) in Windows Explorer's address bar while having
       the folder open.
    5. Run "mkpsxiso.exe <xml filename>" (without the quotes).  It will start 
       building a new .BIN file called mkpsxiso.bin with an accompanying 
       cuefile.

The method above is likely going to change the resulting .BIN file's file size.
This means that the output cannot be used to create a PPF patch, as creating a
PPF patch requires the modified .BIN file to have the same file size as the
original file. 

To get around this, you can use the second method. For this method, you need
psx-mode2. Find psx-mode2-en.exe in the /bin folder.

    1. Move psx-mode2-en.exe to the folder where you have the game's .BIN file.
       Move the modified file to the same folder as well.
       1a. As this method overwrites the existing .BIN file instead of 
           making a new .BIN file, it is recommended that you keep a copy of 
           your .BIN file before proceeding.
    2. Run 'psx-mode2-en.exe <your .BIN file's filename> <path to the modified
       file in the .BIN file> <the modified file's filename>' (without quotes).
       The .BIN file you specified will be modified.
       2a. The 'path to the modified file in the .BIN file' refers to the path
           where the file can be found in the .BIN file. This should begin with
           a backslash. For example, if after extracting the .BIN file, 
           the file you want to change is located in BIN/WORLD/AREA005.EMI, 
           then you need to write '\BIN\WORLD\AREA005.EMI' for this input.

More advanced file modifications, such as expanding a file or adding a new file
is not discussed here, as they involve modifying the game's file sector table,
though the second method can be used to insert a file that is slightly larger or
smaller than the original file. 

            ========================================================
                            1.3. Creating a PPF patch
            ========================================================

Note that this will only work if you used the second method described above, 
and you did not change the file size of any file in the .BIN.

    1. Open PPFStudio, and enter the location where you want to save the PPF
       patch in the 'PPF File (Output)' section.
    2. Open the original BoF4 binary file for the 'Original file', and open the
       CDMage output for the 'Patched file'. This file must have the same size
       as the original file.
    3. Press 'Create PPF Patch' and the patch will be created in the destination
       folder.



[FSTR]--------------------------------------------------------------------------

2. Folder and file structure

--------------------------------------------------------------------------------

After extracting the game's binary file, you should see a folder called 'BIN'
in the folder where you chose to extract the files. This is where the game's
main files are located.
Most of the files relevant to this document are located in the BWORLD folder,
which contains the AREAB and BOSS files used to define a battle location.


            ========================================================
                            2.1. .EMI file structure
            ========================================================

Credit goes to to Mr. Mouse @ forum.xentax.com for figuring out how the
ToC works:
https://forum.xentax.com/viewtopic.php?p=17158&sid=65c631e406bca85f77fc78af36542
4e0#p17158

An .EMI file is kind of a file archive - it contains various files and sections
such as character sprites, textures, enemy data, and in-game text stitched
together into a single file. 

To help locate where each file/section is, a table of contents (henceforth
'ToC') is placed at the beginning of every .EMI file. The ToC is 2048
bytes long, and each entry in the ToC is 16 bytes. The very first entry
is known as the ToC header.
Here is the structure of a ToC:

    ToC header:
        - uint32              Number of files/sections in the .EMI file
        - uint32              Unknown
        - char[8]             Magic number ('MATH_TBL')

    ToC entry:
        - uint32              Size of section/file in bytes
        - uint32              Location of section in the RAM
        - uint32              First four bytes of the section
        - uint16              Unknown
        - 2F 2F               Padding

All integers in the ToC are little-endian, which means that the hexadecimal
representation of the integer must be read backwards. For example, 0e f0 00 00
is read as 0xf00e (decimal 61454), not 0x0ef0000000 (decimal 64156073984).

Here is an example of a ToC header:

    0E 00 00 00 01 00 00 00 4D 41 54 48 5F 54 42 4C

Where:
    - 0E 00 00 00 is a little-endian integer, indicating how many
      files/sections there are in the .EMI file. In this case, there
      are 0x0e (decimal 14) files/sections.
    - 01 00 00 00 serves an unknown purpose.
    - 4D 41 54 48 5F 54 42 4C is the magic number for .EMI files.
      It is the ASCII encoding of the string 'MATH_TBL'.

Here is an example of a ToC entry:

    6D 5F 00 00 00 00 01 80 42 02 45 02 00 00 2E 2E

Where:
    - 6D 5F 00 00 is a little-endian integer, indicating the size
      of the file/section. In this case, the file/section is 0x6D5F
      bytes long, or 24429 in decimal.
    - 00 00 01 80 indicates where the file is stored in the RAM.
      In this case, the file/section is stored at 0x80010000.
    - 42 02 45 02 indicates this file/section's first four bytes
    - 00 00 serves an unknown purpose.
    - 2E 2E is a padding, added so that each entry is exactly 16 bytes.

In the English (NTSC) version of the game, enemy sections are marked as stored
in 00 58 1F 80 (or 0x801F5800).



[FORM]--------------------------------------------------------------------------

3. Enemy formation data

--------------------------------------------------------------------------------

The first 160 bytes of the enemy data section is reserved for enemy formation
data. There are 8 possible formations per AREAB file. An entry for a single
formation is 20 bytes long. Each formation is structured like so:

    __ __ __       __ __
    ID YY XX (x 6) RR RF

Where
    - ID is the index of the enemy's data in the enemy table. It specifies the
      enemy to be included in the formation.
    - YY is the position of the enemy on the Y axis
    - XX is the position of the enemy on the Z axis
    - RR is the encounter rate of this formation when playing with Ryu's party
    - RF is the encounter rate of this formation when playing with Fou-lu

ID, YY and XX together constitute an enemy slot. To mark an empty enemy slot,
ID is set to 0xFF. A single formation can have up to six enemies.
The encounter rate of a formation is divided by 32 to get the actual probability
of encountering that formation. The total encounter rates must therefore add up 
to 32.

Boss areas have the same structure. However, the encounter rate is set to
32/32. Having more than one boss of the same type in a formation crashes the 
game.



[DATA]--------------------------------------------------------------------------

4. Enemy data

--------------------------------------------------------------------------------

Enemy data begins after formation data. Each enemy data is 264 bytes long. The
following is an outline of an enemy data entry:

    - name (8 bytes, +0)
    - ID (1 byte, +8)
    - type (1 byte, +9)
    - default action weight index (1 byte, +10)
    - padding? (1 byte, +11)
    - default behavior (2 bytes, +12)
    - default status (2 bytes, +14) 
    - zenny drop (2 bytes/16-bit integer, +16)
    - EXP drop (2 bytes/16-bit integer, +18)
    - unknown I (2 bytes, +20)
    - LVL (2 bytes/16-bit integer, +22)
    - unknown II (8 bytes, +24)
    - initial ability set (8 bytes, +32)
    - HP (4 bytes/32-bit integer, +40)
    - AP (2 bytes/16-bit integer, +44)
    - unknown III (2 bytes, +46)
    - PWR (2 bytes/16-bit integer, +48)
    - DEF (2 bytes/16-bit integer, +50)
    - AGL (2 bytes/16-bit integer, +52)
    - WIS (2 bytes/16-bit integer, +54)
    - steal item ID (1 byte, +56)
    - steal item type (1 byte, +57)
    - steal rate (1 byte, +58)
    - padding? (1 byte, +59)
    - drop item ID (1 byte, +60)
    - drop item type (1 byte, +61)
    - drop rate (1 byte, +62)
    - padding? (1 byte, +63)
    - Enemy AI data (128 bytes, +64)
    - Resistances (12 bytes, +192)
    - unknown IV (4 bytes, +204)
    - unknown V (5 bytes, +208)
    - shield points (1 byte, +213)
    - padding? (21 bytes, +214)
    - animation data (12 bytes, +235)
    - padding? (7 bytes, +247)
    - Unknown VI (2 byte, +254)
    - padding? (9 bytes, +256)

Most sections are described in more depth below. If a section is to be 
interpreted as an integer, then the size is specified as well. The number with 
the plus sign indicates the offset to go to that section.


            ========================================================
                                    4.1. Name
            ========================================================

Enemy names are limited to eight characters. Names can contain colored text
markers, but the palette selection seems to be broken here. Item name 
placeholders work, but they cannot bypass the eight-character limit. More 
information about text can be found in my Breath of Fire IV text hacking guide 
(https://www.romhacking.net/documents/859/)


            ========================================================
                                     4.2. ID
            ========================================================

Enemy IDs uniquely identify each enemy data entry. For example, BeeTroop, when
you initially meet it in battle, uses the enemy ID 19. When its shield is 
broken, it transforms to another enemy, which also has the name BeeTroop, but an
enemy ID of 20. This new BeeTroop has a different sprite and drops nothing. 
Bosses also have IDs which are separate from normal enemies. 

A list of of enemy ID–enemy name correspondences can be found in the 
"bof4enemies_enemy_id.txt" appendix file.


            ========================================================
                                    4.3. Type
            ========================================================

There are eight enemy types, which are also utilized by bosses. They are mostly
used to determine the enemy's weakness. For example, Dragon-type enemies take
additional damage when attacked by the Dragonslayer, including Fou-lu.

    Hexadecimal     Type
    -----------------------------
    0x00            Humanoid
    0x01            Beast
    0x02            Insect
    0x03            Plant
    0x04            Machine
    0x05            Dragon
    0x06            Demon
    0x07            Fly


            ========================================================
                               4.4. Action weights
            ========================================================

If not idle or disabled, an enemy can perform four actions:
    - Attack
    - Guard
    - Use an ability
    - Escape
The probabilities of an enemy performing each of these actions are determined by
this byte, which is the index of a "weight" value. The weight is used to
influence the random number generation, making certain actions more likely and
others less.

There are four possible dice rolls, from 0 to 3 inclusive. Without any behaviors
set, all rolls will cause the enemy to attack. If the enemy is allowed to use
abilities, then rolling a 3 will cause it to use a random ability from its
ability set. If the enemy is allowed to escape, then rolling a 2 will cause it
to escape. If the enemy is allowed to guard, then rolling a 1 will cause it to
guard.

The dice roll algorithm is as follows:

    (weight[weight_idx] >> (2 * random(0,3))) % 4

Where weight_idx is the action weight index, random(0,3) is a random integer 
from 0 to 3, inclusive, the >> operator means the arithmetic left shift, and the
% operator means the modulo.

A list of weights can be found in the "bof4enemies_weights.txt" appendix file


            ========================================================
                        4.5. Default behavior and status
            ========================================================

Behavior and status are bit switches. For example, to enable reverse targetting,
the first byte of the behavior section is set to 0x04. To enable reverse 
targetting and shield, the first byte of the behavior section is set to 0x84,
which means activating both 0x80 and 0x04.

First byte of the behavior section:

    Hexadecimal     Behavior
    ------------------------------------------------
    0x01            (Apparently not used?)
    0x02            Idle
    0x04            Reverse-targetting
    0x08            (Used but effects are unknown)
    0x10            Counts turns
    0x20            Counts combos
    0x40            (Used but effects are unknown)
    0x80            Shield

Second byte of the behavior section:

    Hexadecimal     Behavior
    ------------------------------------------------
    0x01            Guard when a 1 is rolled
    0x02            Escape when a 2 is rolled
    0x04            Use abilities when a 3 is rolled
    0x08            Target opposing party member with lowest HP
    0x10            Able to get lucky strikes
    0x20            Frequent dodges
    0x40            Frequent counters
    0x80            (Apparently not used?)

First byte of the status section:

    Hexadecimal     Status
    ------------------------------------------------
    0x01            Euphoria
    0x02            (Apparently not used?)
    0x04            Blind
    0x08            Silenced
    0x10            Confused
    0x20            Sleeping
    0x40            Poisoned
    0x80            (Ovum if set on the player)

Second byte of the status section:

    Hexadecimal     Status
    ------------------------------------------------
    0x01            (Apparently not used?)
    0x02            (Apparently not used?)
    0x04            (Apparently not used?)
    0x08            (Apparently not used?)
    0x10            (Apparently not used?)
    0x20            Regenerates half HP every turn
    0x40            Hidden
    0x80            Disabled

Combining 0x40 and 0x80 together (0x40 | 0x80 = 0xC0) indicates that the enemy
in question is dead.


            ========================================================
                         4.6. Zenny, EXP, and Unknown I
            ========================================================

These are self-explanatory. Unknown I is added here because the AI action 0x0A
modifies Zenny, EXP and Unknown I, but no enemy appears to modify its Unknown I
stat.


            ========================================================
                            4.7. Initial ability set
            ========================================================

This is the weighted set of abilities an enemy can use initially. An entry adds
a 1/8 chance of that spell being used. That means if the entire ability set is
set to the same byte, then the enemy can only select a single spell when it uses
an ability.


            ========================================================
                                     4.8. HP
            ========================================================

If an enemy's HP is set to exactly 1,000,000, then that enemy becomes 
invincible. Reducing the maximum HP removes the invincibility. This can only be 
done by the 0x05 action.


            ========================================================
                               4.9. Steal and drop
            ========================================================

Steal and drop rates range from 1 to 7:

    Hexadecimal     Probability
    -------------------------------------
    0x00            0/256
    0x01            2/256
    0x02            4/256
    0x03            16/256
    0x04            32/256
    0x05            64/256
    0x06            128/256
    0x07            256/256

Probabilities as stored as the n in n/256. The probability table is stored in 
every single AREAB file. It would be tedious to list the location of all the 
table. Instead, I point the reader to search the following binary string:

    00000200040010002000400080000001

Which is the probability table itself, in two-byte little endian integers.

The item types are as follow:

    Hexadecimal     Type
    -------------------------------------
    0x00            Normal items
    0x01            Weapons
    0x02            Armor
    0x03            Accessorries

For example, "0B 03 07" means that the enemy will drop a Dragon helm (0B and 03)
all the time (07). 

A complete list of item IDs and names can be found in the 
"bof4enemies_inventory.xlsx" appendix file.


            ========================================================
                         4.10. Resistances and Unknown IV
            ========================================================

Resistances scale from 0 to 7. Unknown IV can be set using AI action 0x09, but
this action is never used by enemies. The following is a table of resistance
values and their effects (credits to Desmond Xie):

Elemental resistances (Fire, Wind, Water, Earth):

    Value   Damage Taken
    ----------------------------------
    0       Receives 200% damage
    1       Receives 150% damage
    2       Receives 100% damage
    3       Receives  75% damage
    4       Receives  50% damage
    5       Receives  25% damage
    6       Receives   0% damage
    7       Absorbs damage      


            ========================================================
                               4.11. Shield points
            ========================================================

If an enemy has its shield activated (by activating 0x80 in the first behavior
byte), then melee and ranged damages are reduced. This does not last 
indefinitely, and shield points are used to measure how much of that shield is
remaining. 

Melee or ranged attacks chip away some shield points. How much each attack
takes away depends on the weapon. For example, every successful melee attack
performed while wielding the Earthbreaker reduces an enemy's shield points by 6.

A complete list of weapons with their shield damaging points can be found in the
"bof4enemies_inventory.xlsx" appendix file.



[AINT]--------------------------------------------------------------------------

5. AI

--------------------------------------------------------------------------------

Normally, enemies start out with a default AI entry, and can have up to 8
entries in the AI table. Some bosses do not use the AI table. Instead, their AI
is set by a separate function.

An AI table entry has the following structure:
    
    - condition (1 byte, +0)
    - action (1 byte, +1)
    - argument for condition (1 byte, +2)
    - padding (1 byte, +3)
    - pointer to message (2 bytes/16-bit integer, +4)
    - padding (2 bytes, +6)
    - arguments for action (8 bytes, +8)

Numbers with a plus sign before them indicate offsets.

            ========================================================
                                5.1. Condition
            ========================================================

An AI entry is activated when the condition is met. There are 72 conditions, 
of which 70 I have discovered. The remaining 2 are not used by enemies.
Not all eight AI entries have to be used. If an entry is unused, its condition 
is set to 0xFF. Some conditions are repeatable or have repeatable version, some
are not repeatable. Repeated conditions are reset when an enemy transforms to 
another enemy. Some conditions take an argument, others do not do anything with
the argument if they are given one.

Non-repeatable conditions:

    Hexadecimal     Condition
    -----------------------------------------------------------------------
    0x00            When hit by an elemental combo
    0x01            When hit by a fire-elemental attack
    0x02            When hit by a water-elemental attack
    0x03            When hit by a wind-elemental attack
    0x04            When hit by an earth-elemental attack
    0x05            When hit by a holy-elemental attack
    0x06            When hit by a ST-down spell
    0x07            When hit by a status spell
    0x08            When hit by a death-elemental attack
    0x09            When hit by a melee or ranged attack
    0x0A            When hit by a melee attack
    0x0B            When hit by a ranged attack
    0x0C            When hit by any successful attack
    0x0D            When hit by a breath attack
    0x0F            When shield is active but destroyed
    0x10            When item is stolen
    0x11            When hit by the ability specified by the argument
    0x12            When the item specified by the argument is used on me
    0x13            When killed
    0x14            When blinded
    0x15            When poisoned
    0x18            When Ryu's level - my level >= argument
    0x1D            When my level - Ryu's level >= argument
    0x1E            When the current turn number is equal to the turn specified 
                    by the argument
    0x25            When hit by some spells
    0x27            When the number of alive members in the enemy's party has 
                    decreased
    0x28            When the number of alive members in the enemy's party is
                    equal to the number of members the enemy party started with
    0x29            When the enemy the only remaining member in its party
    0x2A            When the enemy is not the only remaining member in its party
    0x77            When killed


Repeatable conditions:

    Hexadecimal     Condition
    -----------------------------------------------------------------------
    0x16            When current HP < max HP/argument
    0x17            When current AP < max AP/argument
    0x19            When there is a party member (including self) whose current 
                    HP <= max HP/argument
    0x1A            When there is a party member (including self) whose current 
                    AP <= max AP/argument
    0x1B            When current HP >= max HP/argument
    0x1C            When current AP >= max AP/argument
    0x1F            When the current turn number is even, except for the 
                    first turn
    0x20            When the current turn number is odd
    0x21            When the turn number is divisible by argument except the
                    first turn
    0x2D            When flag 1 is active
    0x2E            When flag 2 is active
    0x2F            When both flag 1 and flag 2 are active
    0x22            When the turn number is one more than a multiple of the 
                    argument except the first turn
    0x23            When the number of alive members in the player's party
                    is equal to the argument
    0x24            When the number of alive members in the player's party
                    is greater than or equal to the argument
    0x64            When hit by an elemental combo
    0x65            When hit by a fire-elemental attack 
    0x66            When hit by a water-elemental attack
    0x67            When hit by a wind-elemental attack
    0x68            When hit by an earth-elemental attack
    0x69            When hit by a holy-elemental attack
    0x6A            When hit by an ST-down spell
    0x6B            When hit by a status attack
    0x6C            When hit by a death-elemental attack
    0x6D            When hit by a melee or ranged attack
    0x6E            When hit by a melee attack
    0x6F            When hit by a ranged attack
    0x70            When hit by any successful attack
    0x71            When hit by a breath attack
    0x73            When shield is active but destroyed
    0x74            When item is stolen
    0x75            When hit by the ability specified by the argument
    0x76            When the item specified by the argument is used on me
    0x78            When blinded
    0x79            When poisoned
    0x7D            When item is stolen
    0x89            When hit by some spells


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.1.1. Notes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Conditions that check whether or not the enemy has been hit with some sort of
attack, such as 0x01 or 0x6E, require that the attack be successful. This means
that the attack must not be parried by the enemy. Dealing 0 damage points counts 
as a successful attack.

Turns are numbered starting from 0. This is how numberings are done in most
programming languages. Therefore, the second turn has the turn number 1. The
game counts the second turn as an odd turn.

When the argument is used as the divisor, such as in 0x16, the argument cannot
be equal to zero. The game will crash otherwise.

HP and AP based conditions (0x16, 0x17, 0x19, 0x1A, 0x1B and 0x1C) are only
triggered once after the condition is true. Conditions based on the player's
party count (0x23 and 0x24) work in the same way. They can be repeated when 
condition becomes false, and then true again. For example, the condition 
"When current HP < Max HP/2" is triggered once by attacking the enemy until its
HP is less than half of its maximum HP. The enemy can then be healed past 
halfway of its HP and then attacked again until its HP is less than half again, 
which will activate the condition a second time.

Conditions 0x25 and 0x89 are only activated when the enemy is hit by certain 
spells.


            ========================================================
                                  5.2. Actions
            ========================================================

There are 16 actions that can be executed when an AI entry is activated. 14 have
been discovered. 1 is known to be used in a boss battle, but its function is yet
unknown.

Actions:
    
    Hexadecimal     Action
    --------------------------------------------------------------------------
    0x00            Do nothing (except display the message for the entry)
    0x01            Change action weight
    0x02            Change ability set
    0x03            Behavior toggle
    0x04            Status toggle
    0x05            Stat multiplier
    0x06            (Unknown, used by Scias)
    0x07            Change resistances I
    0x08            Change resistances II
    0x09            Change Unknown IV
    0x0A            Change Zenny, EXP and Unknown I
    0x0B            Change steal and drop
    0x0C            Flag toggle
    0x0D            Counter toggle
    0x0E            Transform


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.1. Change action weight (0x01)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This command sets the action weight index to the first input byte.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.2. Change ability set (0x02)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This command sets the enemy's ability set to the abilities specified by the
eight input bytes.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.3. Behavior toggle (0x03)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The first and second input bytes of this command are used to disable the 
behaviors specified by the two bytes. The third and fourth input bytes are used 
to enable the behaviors specified by the two bytes.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.4. Status toggle (0x04)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The first and second input bytes of this command are used to disable the 
status effects specified by the two bytes. The third and fourth input bytes are
used to enable the status effects specified by the two bytes.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.5. Stat multiplier (0x05)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The first and second input bytes of this command are used as a mask to indicate
which stats are going to be affected by this command. If a bit in the mask is
set to 1, then that stat will be modified by the multiplier.

First mask byte:

    Hexadecimal     Affects
    ------------------------------------
    0x01            (Unknown)
    0x02            (Unknown)
    0x04            (Unknown)
    0x08            (Unknown)
    0x10            (Unknown)
    0x20            (Unknown)
    0x40            (Unknown)
    0x80            Max AP

Second mask byte:

    Hexadecimal     Affects
    ------------------------------------
    0x01            Max HP
    0x02            (Unknown)
    0x04            (Unknown)
    0x08            LVL
    0x10            WIS
    0x20            AGL 
    0x40            DEF
    0x80            PWR

The third byte is the multiplier. All affected stats are multiplied by the 
multiplied divided by 10. Therefore, a multiplier of 0x0f means multipliying by
1.5.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.6. Change resistances I (0x07)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This command sets the enemy's last eight resistance values to the eight input
bytes. These resistances are:

    - Fire
    - Wind
    - Water
    - Earth
    - Holy
    - Mind
    - Status
    - Death


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.7. Change resistances II (0x08)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This command sets the enemy's first four resistance values to the first four
input bytes.  These resistances are:

    - Melee
    - Ranged
    - Magical
    - Breath


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.8. Change Zenny, EXP and Unknown I (0x0A)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The first input byte is the multiplier applied to the enemy's Zenny drop. The
second input byte is the multiplier applied to the enemy's EXP drop. The third
byte is the multiplier applied to the enemy's Unknown I. 


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.9. Change steal and drop (0x0B)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The first input of this command is used as a mask to indicate which values 
the command changes. If a bit in the mask is set to 1, then that value will 
be set to the new value. The second byte is entirely unused.

Mask byte:

    Hexadecimal     Affects
    ---------------------------------
    0x01            Steal item
    0x02            Steal rate
    0x04            Drop item
    0x08            Drop rate
    0x10            (Unused)
    0x20            (Unused)
    0x40            (Unused)
    0x80            (Unused)

The third and fourth bytes are used to specifiy the new steal item, with the 
third byte used for the item's ID, and the fourth byte for the item's type.
The fifth and sixth bytes are used to specifiy the new drop item, with the 
fifth byte used for the item's ID, and the sixth byte for the item's type.
The seventh byte is used to specify the new steal rate. The eight byte is used
to specify the new drop rate.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.10. Flag toggle (0x0C)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Flags are used to communicate messages or conditions between enemies. There are
two flags which can be set. When an enemy sets a flag to on, another enemy can 
detect this with the appropriate condition byte.

The command takes the input bytes as booleans. Thus, 0 is means to false and
any other number means true. The first input byte enables the first flag, 
while the second input byte enables the second flag. The third byte disables the
first flag, while the fourth input byte disables the second flag. 


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.11. Counter toggle (0x0D)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Some enemies can count turns and combo hits inflicted on it. These are actually
countdowns, with the turn counter decreasing for every turn, and the combo 
counter decreasing for every combo hits received.

For example, if an enemy wants to perform an action when it is hit by a 5-point
combo, it can set the combo counter to 10, and then check when the counter 
reaches 5. Setting the combo counter to 7 and checking when the counter reaches
2 also works.

The first input byte is the turn counter number. The second input byte is the
combo counter number. The third and fourth bytes are booleans which are used to
enable the turn and combo counters respectively. The fifth and sixth byte are
booleans which are used to disable the turn and combo counters respectively.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5.2.11. Transform (0x0E)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This command changes an enemy to another enemy. The first input byte is used to
indicate the ID of the new enemy. For example, BeeTroop transforms to BeeTroop
(with an enemy ID of 20) when its shield is broken. The second byte is a mask 
that specifies what to take from the new enemy and what to keep from the old 
enemy.

Mask bytes:
    Hexadecimal         Effect
    ------------------------------------------------------
    0x01                Preserve steal and drop items
    0x02                Preserve resistances
    0x04                Preserve PWR, DEF, AGL, WIS
    0x08                Preserve HP, AP
    0x10                Change behavior
    0x20                Change status
    0x40                (Unknown)
    0x80                Change name

Enemies can only transform into other enemies defined in the same AREAB file.


            ========================================================
                                  5.3. Messages
            ========================================================

When an AI entry is triggered, an accompanying message can be displayed. The
enemy messages are stored in \BIN\BATTLE\AB000_00.EMI. The fourth and fifth 
bytes of the AI table entry are used to specify the index of the message.

If multiple enemies are supposed to display the same message, then the message
will only displayed once.



[BOSS]--------------------------------------------------------------------------

6. Special bosses

--------------------------------------------------------------------------------

            ========================================================
                                  7.1. I and II
            ========================================================

I and II do not have AI table entries. Instead, their ability sets are 
determined by a random number generator.


            ========================================================
                                   7.2. Fou-lu
            ========================================================

The first battle with Fou-lu is scripted. His initial ability set is 8/8 Patoh,
but his action weight disallows him from using abilities. The whiteout ability
is not caused by the AI table.



[CLOG]--------------------------------------------------------------------------

7. Changelog

--------------------------------------------------------------------------------

26/03/2023      v.Public beta 1     Assembled the easy ones from the text file,
                                    first public beta.
28/03/2023      v.Public beta 2     Completed AI section with what is known so
                                    far.
31/03/2023      v1.0.               First release.
15/10/2023      v1.1.               More information about the drop/steal rate
                                    and some copyediting.